package com.hamster.tools.i18nManger.view
{
	import com.hamster.tools.i18nManger.events.AppEvent;
	import com.hamster.tools.i18nManger.mediator.AppMediator;
	import com.hamster.tools.i18nManger.model.PropertyItemVOProxy;
	import com.hamster.tools.i18nManger.vo.PropertyItemVO;
	
	import mx.managers.PopUpManager;
	
	import org.puremvc.as3.interfaces.INotification;
	import org.puremvc.as3.patterns.mediator.Mediator;
	
	public class PropertyListViewMediator extends Mediator
	{
		public static const NAME:String = "PropertyListViewMediator";
		private var _propertyEditorView:PropertyEditorView;
		
		public function PropertyListViewMediator(viewComponent:Object=null)
		{
			super(NAME, viewComponent);
		}
		
		override public function onRegister():void
		{
			app.addEventListener(AppEvent.OPEN_ITEM_EDITOR_VIEW, openItemEditorViewHandler);
		}
		
		override public function listNotificationInterests():Array
		{
			return [
				AppEvent.SCAN_TARGET_FOLDER_RESPONSE
			];
		}
		
		override public function handleNotification(notification:INotification):void
		{
			var body:Object = notification.getBody();
			var name:String = notification.getName();
			switch (name) {
				case AppEvent.SCAN_TARGET_FOLDER_RESPONSE:
					handleScanTargetFolderResponseHandler(body);
					break;
			}
		}
		
		private function handleScanTargetFolderResponseHandler(data:Object):void
		{
			
		}
		
		private function openItemEditorViewHandler(event:AppEvent):void
		{
			var item:PropertyItemVO = event.data;
			
			_propertyEditorView = PopUpManager.createPopUp(appMediator.app, PropertyEditorView, true) as PropertyEditorView;
			PopUpManager.centerPopUp(_propertyEditorView);
			_propertyEditorView.itemVO = item;
			_propertyEditorView.targetLanguage = app.language;
			_propertyEditorView.addEventListener(AppEvent.SAVE_CLICK, editorViewSaveClickHandler);
			_propertyEditorView.addEventListener(AppEvent.CANCEL_CLICK, editorViewCancelClickHandler);
			var mediator:PropertyEditorViewMediator = new PropertyEditorViewMediator(_propertyEditorView);
			facade.registerMediator(mediator);
		}
		
		private function editorViewSaveClickHandler(event:AppEvent):void
		{
			propertyItemVOProxy.addedItemList.addItem(event.data);
			PopUpManager.removePopUp(_propertyEditorView);
			facade.removeMediator(PropertyEditorViewMediator.NAME);
		}
		
		private function editorViewCancelClickHandler(event:AppEvent):void
		{
			PopUpManager.removePopUp(_propertyEditorView);
			facade.removeMediator(PropertyEditorViewMediator.NAME);
		}
		
		public function get app():PropertyListView
		{
			return viewComponent as PropertyListView;
		}
		
		public function get appMediator():AppMediator
		{
			return this.facade.retrieveMediator(AppMediator.NAME) as AppMediator;
		}
		
		public function get propertyItemVOProxy():PropertyItemVOProxy
		{
			return this.facade.retrieveProxy(PropertyItemVOProxy.NAME) as PropertyItemVOProxy;
		}
	}
}